package com.flying.carclub.api;

import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.http.ApiHandleParams;
import com.flying.carclub.http.ApiHandler;
import com.flying.carclub.http.BaseApiHelper;
import com.flying.carclub.http.BaseBean;
import com.flying.carclub.http.OkHttpStatus;
import com.flying.carclub.http.OkRequestParams;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.ResponseParser;

/**
 * Created by zhuxiaocheng on 2018/1/23.
 */

public class ApiImpl extends BaseApiHelper {
    private static String URL_LOGIN = "http://47.97.167.203/api/user/login";
    private static String URL_SINGUP = "http://47.97.167.203/api/user/signup";
    private static String URL_USER_UPDATE = "http://47.97.167.203/api/user/update";
    private static String URL_FEEDBACK = "http://47.97.167.203/api/user/feedback";

    public void httpLogin(String phone, String pwd, final OnResponseListener listener) {
        OkRequestParams params = new OkRequestParams(URL_LOGIN);
        params.addBodyParameter("username", phone);
        params.addBodyParameter("password", pwd);
        httpPost(params, new ApiHandler() {
            @Override
            public void onSuccess(String result) {
                if (ResponseParser.isRequestSuccess(result)) {
                    UserInfoBean userInfoBean = ResponseParser.parseUserInfo(result);
                    listener.onSuccess(userInfoBean);
                } else {
                    String err = ResponseParser.parseMessage(result);
                    listener.onError(err);
                }
            }

            @Override
            public void onFail(OkHttpStatus status) {
                listener.onError(null);
            }
        });
    }

    public void httpSignup(String phone, String pwd, final OnResponseListener listener) {
        OkRequestParams params = new OkRequestParams(URL_SINGUP);
        params.addBodyParameter("username", phone);
        params.addBodyParameter("password", pwd);
        httpPost(params, new ApiHandler() {
            @Override
            public void onSuccess(String result) {
                if (ResponseParser.isRequestSuccess(result)) {
                    UserInfoBean userInfoBean = ResponseParser.parseUserInfo(result);
                    listener.onSuccess(userInfoBean);
                } else {
                    String err = ResponseParser.parseMessage(result);
                    listener.onError(err);
                }
            }

            @Override
            public void onFail(OkHttpStatus status) {
                listener.onError(null);
            }
        });
    }

    public void httpUpdateUserInfo(String username, String key, String value, final OnResponseListener listener) {
        OkRequestParams params = new OkRequestParams(URL_USER_UPDATE);
        params.addBodyParameter("username", username);
        params.addBodyParameter(key, value);
        httpPost(params, new ApiHandler() {
            @Override
            public void onSuccess(String result) {
                if (ResponseParser.isRequestSuccess(result)) {
                    UserInfoBean userInfoBean = ResponseParser.parseUserInfo(result);
                    listener.onSuccess(userInfoBean);
                } else {
                    String err = ResponseParser.parseMessage(result);
                    listener.onError(err);
                }
            }

            @Override
            public void onFail(OkHttpStatus status) {
                listener.onError(null);
            }
        });
    }

    public void httpFeedback(String content, String phone, final OnResponseListener listener) {
        OkRequestParams params = new OkRequestParams(URL_FEEDBACK);
        params.addBodyParameter("content", content);
        params.addBodyParameter("phone", phone);
        httpPost(params, new ApiHandler() {
            @Override
            public void onSuccess(String result) {
                listener.onSuccess(null);

            }

            @Override
            public void onFail(OkHttpStatus status) {
                listener.onError(null);
            }
        });
    }
}
